import { Component, Input, OnInit } from '@angular/core';
import { OtcShopService } from '@core/routes-services/otc-shop.service';
import { OTC_SHOP_CUS_ROLE, SHOP_AUDITING_STATUS_OPTION } from '../../../../../constant/dictionary';
import { MyModalHelper } from '@core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';
import { OtcCusShopRoleComponent } from '../../../otc-cus-shop-role/otc-cus-shop-role.component';

@Component({
  selector: 'app-otc-cus-shop',
  templateUrl: './otc-cus-shop.component.html',
})
export class OtcCusShopComponent implements OnInit {
  @Input() customerId:any;
  isLoading = false;
  keyword: any;
  status: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  shopAuditingStatus: any [] = SHOP_AUDITING_STATUS_OPTION;
  otcShopCusRole: any [] = OTC_SHOP_CUS_ROLE;

  constructor(private message: NzMessageService,
              private otcShopService: OtcShopService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  initPage() {
    this.isLoading = true;
    this.otcShopService.cusPagination(this.page, this.keyword, this.status, this.customerId).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }


  updateCusShopRole = (entity) => {
    if(entity.role == 3){
      this.message.warning("店铺业务经理角色不能修改");
      return;
    }
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OtcCusShopRoleComponent, { entity: bak}, 500, {
      nzTitle: '修改用户店铺角色',
    }).subscribe(result => {
      if (result) {
        this.message.success('修改成功');
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

}
